﻿//********************************************************************************//
//创建日期：<创建日期，2016-10-10>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：计划查询
//********************************************************************************//
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;


namespace Web.Pages.QuShui.DiXiaS.ChaXunGL
{
    public partial class JiHuaCX : BasePage
    {
        #region 属性

        /// <summary>
        /// 保存结果集1
        /// </summary>
        private DataTable dtGridTab1
        {
            get
            {
                return ViewState["dtGridTab1"] as DataTable;
            }
            set
            {
                ViewState["dtGridTab1"] = value;
            }
        }

        /// <summary>
        /// 保存结果集2
        /// </summary>
        private DataTable dtGridTab2
        {
            get
            {
                return ViewState["dtGridTab2"] as DataTable;
            }
            set
            {
                ViewState["dtGridTab2"] = value;
            }
        }

        /// <summary>
        /// 保存结果集3
        /// </summary>
        private DataTable dtGridTab3
        {
            get
            {
                return ViewState["dtGridTab3"] as DataTable;
            }
            set
            {
                ViewState["dtGridTab3"] = value;
            }
        }
        /// <summary>
        /// 保存结果集4
        /// </summary>
        private DataTable dtGridTab4
        {
            get
            {
                return ViewState["dtGridTab4"] as DataTable;
            }
            set
            {
                ViewState["dtGridTab4"] = value;
            }
        }
        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _SpHelper = new SpHelper();
        /// <summary>
        /// 行政区
        /// </summary>
        QS_JX_XINGZHENGQBLL XZQ = new QS_JX_XINGZHENGQBLL();
       
        /// <summary>
        /// 管理部门
        /// </summary>
        QS_JX_BUMENXXBLL GLBM = new QS_JX_BUMENXXBLL();
      
        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
            }
            gvGridTab1.DataSource = this.dtGridTab1;
            gvGridTab1.DataBind();

            gvGridTab2.DataSource = this.dtGridTab2;
            gvGridTab2.DataBind();

            gvGridTab3.DataSource = this.dtGridTab3;
            gvGridTab3.DataBind();

            gvGridTab4.DataSource = this.dtGridTab4;
            gvGridTab4.DataBind();
        }




        /// <summary>
        /// 查询信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnChaXun_Click(object sender, EventArgs e)
        {
            if (cmbYear.Text != "")
            {
                int wtype = BusinessTool.WType;//1 地表水，2 地下水
                string y =  cmbYear.Text;
                string hnhw = cmbHuanNeiHW.SelectedIndex <= 0 ?  null: cmbHuanNeiHW.Value.ToString();
                string xzq = cmbXingZhengQ.SelectedIndex <= 0 ?null: cmbXingZhengQ.Value.ToString()  ;
                string ywy = cmbYeWuY.SelectedIndex <= 0 ?null: cmbYeWuY.Value.ToString() ;
                int plantype = rabJiHuaLX.Checked && cbmJiHuaLX.SelectedIndex > 0 ? Convert.ToInt32(cbmJiHuaLX.Value) : 0;
                string yhh = chkYongHuH.Checked && txtYongHuH.Text != "" ? txtYongHuH.Text : null;
                string yhm = chkYongHuM.Checked && txtYongHuM.Text != "" ? txtYongHuM.Text : null;
                string yhdz = chkYongHuDZ.Checked && txtYongHuDZ.Text != "" ? txtYongHuDZ.Text : null;
                string zgbm = rabGuanLiBM.Checked && cbmGuanLiBM.SelectedIndex > 0  ? cbmGuanLiBM.Value.ToString() :null;
                switch (ASPxPageControl1.ActiveTabIndex)
                {
                    case 0:
                        LoadGridData1(wtype, y, hnhw, xzq, ywy, plantype, yhh, yhm, yhdz, zgbm);
                        break;
                    case 1:
                        LoadGridData2(wtype, y, hnhw, xzq, ywy, plantype, yhh, yhm, yhdz, zgbm);
                        break;
                    case 2:
                        LoadGridData3(wtype, y, hnhw, xzq, ywy, plantype, yhh, yhm, yhdz, zgbm);
                        break;
                    case 3:
                        LoadGridData4(wtype, y, hnhw, xzq, ywy, plantype, yhh, yhm, yhdz, zgbm);
                        break;
                    default: break;
                }
               
            }
        }


        /// <summary>
        /// tab1导出excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            if (this.dtGridTab1 == null || this.dtGridTab1.Rows.Count < 1)
            {
                PageTools.AlertMsg(this, "没有符合的数据导出");
            }
            else
            {
                PageTools.ExportExcel(this.dtGridTab1);
            }
        }


        #endregion

        #region 方法

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            int wtype = BusinessTool.WType;
            gvGridTab1.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            gvGridTab2.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            gvGridTab3.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            gvGridTab4.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            //水量年月
            int water_y_m = BusinessTool.GetSLYMorZWNY(1, wtype);
            for (int i = DateTime.Now.Year - 10; i <= DateTime.Now.Year; i++)
            {
                cmbYear.Items.Add(i.ToString(), i.ToString());
            }

            cmbYear.Value = Convert.ToInt32(water_y_m) / 100;
            

            //加载行政区
            LoadXZQ();
            //加载业务员
            LoadYWY();
            LoadGLBM();

        }
        /// <summary>
        /// 加载管理部门
        /// </summary>
        private void LoadGLBM()
        {
            DataTable dt = GLBM.SelectQS_JX_BUMENXXesDynamic("I_JLZT=0 and S_BUMENBH NOT IN(0,600)", null);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbmGuanLiBM.Items.Add(dt.Rows[i]["S_BUMENMC"].ToString(), dt.Rows[i]["S_BUMENBH"]);
            }
            cbmGuanLiBM.Items.Insert(0, new ListEditItem("", "0"));
        }
        /// <summary>
        /// 加载行政区
        /// </summary>
        private void LoadXZQ()
        {
            DataTable dt = XZQ.SelectQS_JX_XINGZHENGQsDynamic("I_JLZT=0", null);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cmbXingZhengQ.Items.Add(dt.Rows[i]["S_XINGZHENGQM"].ToString(), dt.Rows[i]["S_XINGZHENGQH"]);
            }
        }

        /// <summary>
        /// 加载业务员
        /// </summary>
        private void LoadYWY()
        {
            PageTools.QSLoadCombox(cmbYeWuY, PageTools.eLoadType.业务员QS, null, "");
        }

        /// <summary>
        /// 加载查询数据1
        /// </summary>
        private void LoadGridData1(int wtype, string ym, string hnhw, string xzq, string ywy, int plantype, string yhh, string yhm, string yhdz, string zgbm)
        {
            this.dtGridTab1 = _SpHelper.P_QSCX_JIHUACX(wtype,1, ym, hnhw, xzq, ywy, plantype, yhh, yhm, yhdz, zgbm).Tables[0];
            gvGridTab1.DataSource = this.dtGridTab1;
            gvGridTab1.DataBind();
            int zj = 0;
            if (this.dtGridTab1 != null && this.dtGridTab1.Rows.Count > 0)
            {
                zjlTab1.Text = "共" + this.dtGridTab1.Rows.Count.ToString() + "条";
                gvGridTab1.FocusedRowIndex = 0;
                
                for (int i = 0; i < dtGridTab1.Rows.Count; i++)
                {
                    zj += Convert.ToInt32(dtGridTab1.Rows[i]["I_KAICAIJH"]);
                }
            }
            txtZongLiangTab1.Text = zj.ToString();
        }

        /// <summary>
        /// 加载查询数据2
        /// </summary>
        private void LoadGridData2(int wtype, string ym, string hnhw, string xzq, string ywy, int plantype, string yhh, string yhm, string yhdz, string zgbm)
        {
            this.dtGridTab2 = _SpHelper.P_QSCX_JIHUACX(wtype, 2, ym, hnhw, xzq, ywy, plantype, yhh, yhm, yhdz, zgbm).Tables[0];
            gvGridTab2.DataSource = this.dtGridTab2;
            gvGridTab2.DataBind();
            int zj1 = 0;
            int zj2 = 0;
            if (this.dtGridTab2 != null && this.dtGridTab2.Rows.Count > 0)
            {
                zjlTab2.Text = "共" + this.dtGridTab2.Rows.Count.ToString() + "条";
                gvGridTab2.FocusedRowIndex = 0;

                for (int i = 0; i < dtGridTab2.Rows.Count; i++)
                {
                    zj1 += Convert.ToInt32(dtGridTab2.Rows[i]["I_KAICAIJH"]);
                    zj2 += Convert.ToInt32(dtGridTab2.Rows[i]["I_HUIGUANJH"]);
                }
            }
            txtZongLiangTab2.Text = zj1.ToString();
            txtZongLiang2Tab2.Text = zj2.ToString();
        }

        /// <summary>
        /// 加载查询数据3
        /// </summary>
        private void LoadGridData3(int wtype, string ym, string hnhw, string xzq, string ywy, int plantype, string yhh, string yhm, string yhdz, string zgbm)
        {
            this.dtGridTab3 = _SpHelper.P_QSCX_JIHUACX(wtype, 3, ym, hnhw, xzq, ywy, plantype, yhh, yhm, yhdz, zgbm).Tables[0];
            gvGridTab3.DataSource = this.dtGridTab3;
            gvGridTab3.DataBind();
            int zj = 0;
            if (this.dtGridTab3 != null && this.dtGridTab3.Rows.Count > 0)
            {
                zjlTab3.Text = "共" + this.dtGridTab3.Rows.Count.ToString() + "条";
                gvGridTab3.FocusedRowIndex = 0;

                for (int i = 0; i < dtGridTab3.Rows.Count; i++)
                {
                    zj += Convert.ToInt32(dtGridTab3.Rows[i]["I_ZHUANGUANJH"]);
                }
            }
            txtZongLiangTab3.Text = zj.ToString();
        }

        /// <summary>
        /// 加载查询数据4
        /// </summary>
        private void LoadGridData4(int wtype, string ym, string hnhw, string xzq, string ywy, int plantype, string yhh, string yhm, string yhdz, string zgbm)
        {
            this.dtGridTab4 = _SpHelper.P_QSCX_JIHUACX(wtype, 4, ym, hnhw, xzq, ywy, plantype, yhh, yhm, null, null).Tables[0];
            gvGridTab4.DataSource = this.dtGridTab4;
            gvGridTab4.DataBind();
            int zj = 0;
            if (this.dtGridTab4 != null && this.dtGridTab4.Rows.Count > 0)
            {
                zjlTab4.Text = "共" + this.dtGridTab4.Rows.Count.ToString() + "条";
                gvGridTab4.FocusedRowIndex = 0;

                for (int i = 0; i < dtGridTab4.Rows.Count; i++)
                {
                    zj += Convert.ToInt32(dtGridTab4.Rows[i]["I_TZL"]);
                }
            }
            txtZongLiangTab4.Text = zj.ToString();
        }
       
        #endregion

        protected void btnExport_Click(object sender, EventArgs e)
        {
            int index=ASPxPageControl1.ActiveTabIndex;
            string sName = "";
            if (index==0)
            {
                ASPxGridViewExporter1.GridViewID = "gvGridTab1";
                sName = "开采计划";
                gvGridTab1.Settings.ShowFooter = false;
            }
            else if (index == 1)
            {
                ASPxGridViewExporter1.GridViewID = "gvGridTab2";
                sName = "采灌计划";
                gvGridTab2.Settings.ShowFooter = false;
            }
            else if (index == 2)
            {
                ASPxGridViewExporter1.GridViewID = "gvGridTab3";
                sName = "专灌计划";
                gvGridTab3.Settings.ShowFooter = false;
            }
            else if (index == 3)
            {
                ASPxGridViewExporter1.GridViewID = "gvGridTab4";
                sName = "加价调整量";
                gvGridTab4.Settings.ShowFooter = false;
            }
            
            
            ASPxGridViewExporter1.WriteXlsToResponse(sName);

        }
    }
}